<template>
  <e-table
    ref="eTable"
    :tableData="deptData"
    :columns="deptColumns"
    :loading="loading"
    :pageObj="null"
  >
    <el-table-column v-if="type === 'report'" align="center" label="方式" slot="reportType">
      <template slot-scope="scope">
        <el-radio-group v-model="scope.row.type" size="small">
          <el-radio-button label="0">岗位</el-radio-button>
          <el-radio-button label="1">角色</el-radio-button>
          <el-radio-button label="2">人员</el-radio-button>
        </el-radio-group>
      </template>
    </el-table-column>
    <el-table-column label="通知部门" slot="noticeDept" align="center">
      <template slot-scope="scope">
        <dept-select
          ref="deptSelect"
          :selectValue="scope.row.notifierDeptId"
          v-model="scope.row.dataDept"
          @select="e => handleDeptSelect(e, scope.row, 'dept')"
        />
      </template>
    </el-table-column>
    <el-table-column label="通知" slot="noticePosition" align="center">
      <template slot-scope="scope">
        <el-select
          v-model="scope.row.notifierPositionId"
          @change="e => handleDeptSelect(e, scope.row, 'position')"
        >
          <el-option
            v-for="(item,index ) in positionData"
            :key="index"
            :value="item.positionId"
            :label="item.positionName"
          />
        </el-select>
      </template>
    </el-table-column>
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
// 通知人表格组件和上报表格公用

import DeptSelect from '@/components/DeptSelect'
import DeptPositionS from '@/components/DeptPositionS'
import { openNotification } from '@/utils/util'
import { queryPositionByDeptId } from '@/api/department'

export default {
  components: {
    DeptSelect,
    DeptPositionS
  },
  props: {
    tableData: {
      type: Array,
      default: []
    },
    type: {
      type: String,
      default: ''
    }
  },
  watch: {
    tableData: {
      handler(n, o) {
        this.deptData = [...n]
      }
    }
  },
  data() {
    return {
      loading: false,
      deptData: [],
      deptColumns: [{ slot: 'reportType' }, { slot: 'noticeDept' }, { slot: 'noticePosition' }, { slot: 'action' }],
      positionData: [] // 岗位数组
    }
  },
  methods: {
    async handleDeptSelect(value, row, type) {
      console.log('value', value, row)
      const index = this.deptData.findIndex(v => v.xmId === row.xmId)
      if (type === 'dept') {
        // 预警上报
        // 部门时，调用接口获取部门下的岗位
        if (row.type === '0') {
          const res = await queryPositionByDeptId({ deptId: value.id })
          if (res.state === '000000') {
            this.positionData = res.data
          }
        } else if (row.type === '2') {
          //人员
          const res = await queryPositionByDeptId({ deptId: value.id })
          if (res.state === '000000') {
            this.positionData = res.data
          }
        }

        this.$set(this.deptData[index], 'notifierDeptId', value.id)
      } else {
        this.$set(this.deptData[index], 'notifierPositionId', value.id)
      }
      this.$emit('tableUpdate', this.deptData)
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const index = _this.deptData.findIndex(v => v.xmId === row.xmId)
          _this.deptData.splice(index, 1)
          openNotification('success', '删除成功')
          _this.$emit('tableUpdate', _this.deptData)
        }
      })
    }
  },
  created() {
    this.deptData = [...this.tableData]
  }
}
</script>

<style scoped>
.data-table >>> .cell,
.data-table >>> .el-table,
.data-table >>> .el-table__body-wrapper {
  overflow: inherit;
}
</style>
